import * as Cesium from '@cesiumjs';

/* 162 */
/***/
(function(module, exports, __webpack_require__) {

  "use strict";

  Object.defineProperty(exports, "__esModule", {
    value: true
  });

  var _toConsumableArray2 = __webpack_require__(8);

  var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

  var _getPrototypeOf = __webpack_require__(3);

  var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

  var _classCallCheck2 = __webpack_require__(0);

  var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

  var _createClass2 = __webpack_require__(1);

  var _createClass3 = _interopRequireDefault(_createClass2);

  var _possibleConstructorReturn2 = __webpack_require__(4);

  var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

  var _inherits2 = __webpack_require__(5);

  var _inherits3 = _interopRequireDefault(_inherits2);

  var _mobx = __webpack_require__(2);

  var _XbsjBase = __webpack_require__(7);

  var _XbsjBase2 = _interopRequireDefault(_XbsjBase);

  var _XbsjCzmObj2 = __webpack_require__(11);

  var _XbsjCzmObj3 = _interopRequireDefault(_XbsjCzmObj2);

  var _XbsjObject = __webpack_require__(9);

  var _XbsjObject2 = _interopRequireDefault(_XbsjObject);

  var _HTML = __webpack_require__(21);

  var _HTML2 = _interopRequireDefault(_HTML);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      default: obj
    };
  }

  var xbsjEarthScriptUrl = void 0;

  function createCameraModel(earth) {
    xbsjEarthScriptUrl = xbsjEarthScriptUrl || _HTML2.default.getScriptBaseUrl('XbsjEarth');
    var model = earth.czm.scene.primitives.add(Cesium.Model.fromGltf({
      url: xbsjEarthScriptUrl + 'models/camera1/camera1.gltf?k=path',
      allowPicking: false,
      // cacheKey: Cesium.createGuid(),
      forwardAxis: Cesium.Axis.X
    }));

    // model.opaquePass = Cesium.Pass.TRANSLUCENT;
    model.opaquePass = Cesium.Pass.OVERLAY;
    model.cull = false;
    model.shadows = Cesium.ShadowMode.DISABLED;
    model.luminanceAtZenith = 1.2;
    // model.xbsjForceDepthTestDisabled = true;
    // model.colorBlendMode = Cesium.ColorBlendMode.MIX;
    // model.xbsjForceDepthTestDisabled = true;
    // model.opaquePass = Cesium.Pass.OVERLAY;
    // model.modelMatrix = this._lastMat4;

    model.minimumPixelSize = 50;

    return model;
  }

  function initCameraHelpers(earth) {
    if (!earth._cameraModelsPool) {
      earth._cameraModelsPool = [];

      earth._createCameraModel = function() {
        if (earth._cameraModelsPool.length > 0) {
          return earth._cameraModelsPool.pop();
        } else {
          return createCameraModel(earth);
        }
      };

      earth._destroyCameraModel = function(cameraHelper) {
        earth._cameraModelsPool.push(cameraHelper);
      };

      earth.disposers.push(function() {
        earth._cameraModelsPool.forEach(function(e) {
          earth.czm.scene.primitives.remove(e);
        });
        earth._cameraModelsPool.length = 0;
      });
    }
  }

  /**
   * 内部使用，不对外
   * @memberof Obj
   * @class
   * @extends XbsjCzmObj
   * @ignore
   */

  var CameraHelper = function(_XbsjCzmObj) {
    (0, _inherits3.default)(CameraHelper, _XbsjCzmObj);

    function CameraHelper(earth, guid) {
      (0, _classCallCheck3.default)(this, CameraHelper);

      var _this = (0, _possibleConstructorReturn3.default)(this, (CameraHelper.__proto__ || (0, _getPrototypeOf2
        .default)(CameraHelper)).call(this, earth, guid));

      initCameraHelpers(earth);

      _this.disposers.push(_XbsjBase2.default.registerPosition(_this));
      _this.disposers.push(_XbsjBase2.default.registerRotation(_this));

      _this.disposers.push((0, _mobx.reaction)(function() {
        return {
          position: [].concat((0, _toConsumableArray3.default)(_this.position)),
          rotation: [].concat((0, _toConsumableArray3.default)(_this.rotation))
        };
      }, function() {
        _this._updateCameraPostionAndRotation();
      }));

      _this._cameraModel = undefined;

      var updateShow = function updateShow() {
        var show = _this.show;
        if (show && !_this._cameraModel) {
          _this._cameraModel = earth._createCameraModel();
          _this._updateCameraPostionAndRotation();
          _this._cameraModel.show = true;
        } else if (!show && _this._cameraModel) {
          _this._cameraModel.show = false;
          earth._destroyCameraModel(_this._cameraModel);
          _this._cameraModel = undefined;
        }
      };

      _this.disposers.push((0, _mobx.autorun)(updateShow));

      _this.disposers.push(function() {
        _this.show = false;
        updateShow();
      });
      return _this;
    }

    (0, _createClass3.default)(CameraHelper, [{
      key: '_updateCameraPostionAndRotation',
      value: function _updateCameraPostionAndRotation() {
        if (this._cameraModel) {
          var _Cesium$Cartesian;

          var hpr = new(Function.prototype.bind.apply(Cesium.HeadingPitchRoll, [null].concat((0,
            _toConsumableArray3.default)(this.rotation))))();
          hpr.heading -= Cesium.Math.PI_OVER_TWO;
          var center = (_Cesium$Cartesian = Cesium.Cartesian3).fromRadians.apply(_Cesium$Cartesian, (0,
            _toConsumableArray3.default)(this.position).concat([undefined, new Cesium.Cartesian3()]));
          var hprToFF = Cesium.Transforms.headingPitchRollToFixedFrame;
          hprToFF(center, hpr, undefined, undefined, this._cameraModel.modelMatrix);
        }
      }
    }, {
      key: 'isCameraLike',
      get: function get() {
        return true;
      }
    }]);
    return CameraHelper;
  }(_XbsjCzmObj3.default);

  CameraHelper.defaultOptions = {
    position: [0, 0, 0],
    rotation: [0, 0, 0],
    show: false
  };

  _XbsjObject2.default.registerType(CameraHelper, 'CameraHelper');

  exports.default = CameraHelper;

  /***/
})
